package com.example.exam.controller;

import com.alibaba.fastjson.JSON;
import com.example.exam.entity.*;
import com.example.exam.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/roa")
public class RandomOrAppointExamController {
    @Autowired
    private ChoiceService choiceService;
    @Autowired
    private FillbacksService fillbacksService;
    @Autowired
    private TorfService torfService;
    @Autowired
    private SubjectiveService subjectiveService;
    @Autowired
    private PaperService paperService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private MajorService majorService;

    /**
     * 批量插入选择题
     *
     * @param lists 传入题
     * @return 返回整形 1为成功，0为失败
     */
    @RequestMapping("/chooseChoice")
    public int insertChoice(@RequestParam("lists[]") List<Integer> lists, HttpSession session) {
        int flag = (int) session.getAttribute("flag");
        if (flag == 5) {
            return 0;
        } else {
        int questionId = (int) session.getAttribute("questionId");
        int paperid = questionService.queryById(questionId).getPaperid();
        if (paperService.queryById(paperid) == null) {
            paperService.insert2(paperid);
        }
        Paper paper = paperService.queryById(paperid);
        String paperQuestion = paper.getPaperquestion();
        if (paperQuestion == null) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < lists.size() - 1; i++) {
                sb.append(lists.get(i) + ",");
            }
            sb.append(lists.get(lists.size() - 1));
            paper.setPaperquestion(sb.toString());
        } else {
            StringBuilder sb = new StringBuilder(paperQuestion);
            sb.append(",");
            for (int i = 0; i < lists.size() - 1; i++) {
                sb.append(lists.get(i) + ",");

            }
            sb.append(lists.get(lists.size() - 1));
            paper.setPaperquestion(sb.toString());
        }
        int flags = this.paperService.update1(paper);
        session.setAttribute("flag", flags);
        return flags;
    }}

    /**
     * 批量插入填空题
     *
     * @param lists 传入题
     * @return 返回整形 1为成功，0为失败
     */
    @RequestMapping("/chooseFillbacks")
    public int insertFillbacks(@RequestParam("lists[]") List<Integer> lists, HttpSession session) {
        int flag = (int) session.getAttribute("flag");
        if (flag == 5) {
            return 0;
        } else {
        int questionId = (int) session.getAttribute("questionId");
        int paperid = questionService.queryById(questionId).getPaperid();
        if (paperService.queryById(paperid) == null) {
            paperService.insert2(paperid);
        }
        Paper paper = paperService.queryById(paperid);
        String paperQuestion = paper.getPaperquestion();
        if (paperQuestion == null) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < lists.size() - 1; i++) {
                sb.append(lists.get(i) + ",");
            }
            sb.append(lists.get(lists.size() - 1));
            paper.setPaperquestion(sb.toString());
        } else {
            StringBuilder sb = new StringBuilder(paperQuestion);
            sb.append(",");
            for (int i = 0; i < lists.size() - 1; i++) {
                sb.append(lists.get(i) + ",");

            }
            sb.append(lists.get(lists.size() - 1));
            paper.setPaperquestion(sb.toString());
        }
        int flags = this.paperService.update1(paper);
        session.setAttribute("flag", flags);
        return flags;
    }}

    /**
     * 批量插入判断题
     *
     * @param lists 传入题
     * @return 返回整形 1为成功，0为失败
     */
    @RequestMapping(value = "/chooseTorf")
    public int insertTorf(@RequestParam("lists[]") List<Integer> lists, HttpSession session) {
        int flag = (int) session.getAttribute("flag");
        if (flag == 5) {
            return 0;
        } else {
        int questionId = (int) session.getAttribute("questionId");
        int paperid = questionService.queryById(questionId).getPaperid();
        if (paperService.queryById(paperid) == null) {
            paperService.insert2(paperid);
        }
        Paper paper = paperService.queryById(paperid);
        String paperQuestion = paper.getPaperquestion();
        if (paperQuestion == null) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < lists.size() - 1; i++) {
                sb.append(lists.get(i) + ",");
            }
            sb.append(lists.get(lists.size() - 1));
            paper.setPaperquestion(sb.toString());
        } else {
            StringBuilder sb = new StringBuilder(paperQuestion);
            sb.append(",");
            for (int i = 0; i < lists.size() - 1; i++) {
                sb.append(lists.get(i) + ",");

            }
            sb.append(lists.get(lists.size() - 1));
            paper.setPaperquestion(sb.toString());
        }
        int flags = this.paperService.update1(paper);
        session.setAttribute("flag", flags);
        return flags;
    }}

    /**
     * 批量插入主观题
     *
     * @param lists 传入题
     * @return 返回整形 1为成功，0为失败
     */
    @RequestMapping("/chooseSubjective")
    public int insertSubjective(@RequestParam("lists[]") List<Integer> lists, HttpSession session) {
        int flag = (int) session.getAttribute("flag");
        if (flag == 5) {
            return 0;
        } else {
        int questionId = (int) session.getAttribute("questionId");
        int paperid = questionService.queryById(questionId).getPaperid();
        if (paperService.queryById(paperid) == null) {
            paperService.insert2(paperid);
        }
        Paper paper = paperService.queryById(paperid);
        String paperQuestion = paper.getPaperquestion();
        if (paperQuestion == null) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < lists.size() - 1; i++) {
                sb.append(lists.get(i) + ",");
            }
            sb.append(lists.get(lists.size() - 1));
            paper.setPaperquestion(sb.toString());
        } else {
            StringBuilder sb = new StringBuilder(paperQuestion);
            sb.append(",");
            for (int i = 0; i < lists.size() - 1; i++) {
                sb.append(lists.get(i) + ",");

            }
            sb.append(lists.get(lists.size() - 1));
            paper.setPaperquestion(sb.toString());
        }
        int flags = this.paperService.update1(paper);
        session.setAttribute("flag", flags);
        return flags;
    }}

    /**
     * 指定题目出题
     *
     * @param
     * @return
     */
    @RequestMapping("/appoint")
    public String appoint(HttpSession session) {
        int flag = (int) session.getAttribute("flag");
        if (flag != 4) {
            return JSON.toJSONString("未完全录入");
        } else {
            int questionId = (int) session.getAttribute("questionId");
            int paperid = questionService.queryById(questionId).getPaperid();
            String paperquestion = paperService.queryById(paperid).getPaperquestion();
            String[] split = paperquestion.split(",");
            return JSON.toJSONString(split);
        }
    }

    /**
     * @param //session
     * @return
     */
    @RequestMapping("/allquestion")
    public List<Question> allquestion(){//HttpSession session) {
        Integer teacherId = 3003;//(Integer) session.getAttribute("userId");
        Teacher teacher = teacherService.queryById(teacherId);
        Integer collegeId = teacher.getcollegeId();

        List<Integer> majorIds = new LinkedList<>();
        List<Major> majors = majorService.queryAll();
        for (Major major : majors) {
            if (major.getCollegerid().equals(collegeId)) {
                majorIds.add(major.getMajorid());
            }
        }
        Set<Question> res = new HashSet<>();
        List<Question> questions = questionService.queryAll2();
        for (int i = 0; i < majorIds.size(); i++) {
            for (int j = 0; j < questions.size(); j++) {
                if (questions.get(j).getMajorid().equals(majorIds.get(i))) {
                    res.add(questions.get(j));
                }
            }
        }
        List<Question> ans = new LinkedList<>();
        for (Question re : res) {
            ans.add(re);
        }
        return ans;
    }

    @RequestMapping("/random")
    public String random(HttpSession session) {
        int flag = (int) session.getAttribute("flag");
        if (flag == 1) {
            return JSON.toJSONString("已指定部分题目，请选择指定出题");
        } else {
            int questionId = (int) session.getAttribute("questionId");
            int paperid = questionService.queryById(questionId).getPaperid();
            if (paperService.queryById(paperid) == null) {
                paperService.insert2(paperid);

            }
            Paper paper = paperService.queryById(paperid);
            StringBuilder sb = new StringBuilder();
            List<Choice> choices = choiceService.queryAll();
            List<Fillbacks> fillbacks = fillbacksService.queryAll();
            List<Torf> torfs = torfService.queryAll();
            List<Subjective> subjectives = subjectiveService.queryAll();

            int choiceSize = choices.size();
            int fillSize = fillbacks.size();
            int torfSize = torfs.size();
            int subSize = subjectives.size();

            for (int i = 0; i < 5; i++) {
                int result = (int)(Math.random() * choiceSize) ;
                choiceSize--;
                sb.append(choices.get(result).getChid() + ",");
                choices.remove(result);
            }
            for (int i = 0; i < 5; i++) {
                int result = (int)( Math.random() * fillSize);
                fillSize--;
                sb.append(fillbacks.get(result).getQno() + ",");
                fillbacks.remove(result);
            }
            for (int i = 0; i < 5; i++) {
                int result = (int)(Math.random() * torfSize);
                torfSize--;
                sb.append(torfs.get(result).getQno() + ",");
                torfs.remove(result);
            }
            for (int i = 0; i < 1; i++) {
                int result = (int)( Math.random() * subSize);
                subSize--;
                sb.append(subjectives.get(result).getSubid() + ",");
                subjectives.remove(result);
            }
            sb.append(subjectives.get(0).getSubid());
            paper.setPaperquestion(sb.toString());
            this.paperService.update1(paper);
            session.setAttribute("flag", 5);
            return JSON.toJSONString(sb.toString());
        }
    }
}
